﻿using Abp.AutoMapper;
using Hicap.QRCodeWarehouse.Application.Dto;
using Hicap.QRCodeWarehouse.Core.Managers;
using Hicap.QRCodeWarehouse.Core.Models.DbSet;
using System.Threading.Tasks;

namespace Hicap.QRCodeWarehouse.Application.App
{
    /// <summary>
    /// Codes generation task service
    /// </summary>
    public class GenerationTaskAppService : QrCodeWarehouseApplicationBase<GenerationTask, GenerationTaskListDto, GenerationTaskEditDto>
    {
        private readonly IGenerationTaskManager _manager;

        public GenerationTaskAppService(IGenerationTaskManager manager) : base(manager)
        {
            _manager = manager;
        }

        /// <summary>
        /// Start a manual task
        /// </summary>
        /// <param name="input">GenerationTaskDto</param>
        /// <returns></returns>
        public async Task<GenerationTaskListDto> UpdateManualTaskActive(GenerationTaskDto input)
        {
            GenerationTask entity = await _manager.StartTask(input.TaskId);
            return entity.MapTo<GenerationTaskListDto>();
        }

        /// <summary>
        /// Active/Deactive a timed task
        /// </summary>
        /// <param name="input">GenerationTaskActiveDto</param>
        /// <returns></returns>
        public async Task UpdateTimedTaskActive(GenerationTaskActiveDto input)
        {
            await _manager.SwitchActive(input.TaskId, input.IsActive);
        }

        /// <summary>
        /// Create a timed task and immediately  active it
        /// </summary>
        /// <param name="task">GenerationTaskEditDto</param>
        /// <returns></returns>
        public async Task UpsertAndActiveTimedTask(GenerationTaskEditDto task)
        {
            await _manager.PutTimedTask(task.MapTo<GenerationTask>());
        }
    }
}